package com.kingyea.mobilepolice.zuul.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description 状态码类
 * @date Created in 2017/12/29 9:51
 */
public class StateCode {

    private static final Logger LOGGER = LoggerFactory.getLogger(StateCode.class);

    /** S开头表示系统常量码 ; B开头表示业务常量码   */

    /**
     * 操作成功
     */
    public static String OPERATION_SUCCESS = "SZUUL100101";

    /**
     * 系统异常，请稍后再试
     */
    public static String SYSTEM_ERROR = "SZUUL100500";
    /**
     * 参数不合法
     */
    public static String ILLEGAL_ARGUMENT_ERROR = "SZUUL100501";
    /**
     * 参数缺失
     */
    public static String NO_ARGUMENT_ERROR = "SZUUL100502";

    /**
     * 数据异常，找不到对应记录
     */
    public static String SERVER_DATA_MISSING_ERROR = "SZUUL100503";
    /**
     * 数据异常，记录重复
     */
    public static String SERVER_DATA_DUPLICATE_ERROR = "SZUUL100504";
    /**
     * 系统数据异常
     */
    public static String SERVER_DATA_ERROR = "SZUUL100505";

    public static String SERVER_MISSING_ERROR = "SZUUL100506";


    /** -------------------------业务错误码---------------------- */

    /**
     * 非法用户
     */
    public static String ILLEGAL_USER_ERROR = "BZUUL102001";


    private static Map<String, String> codeMsgMap = new HashMap<>();

    static {
        try {
            // 数据来源 properties
            Properties proper = new PropertiesUtil("stateCode.properties").getProperties();
            String codeMsgSuffix = "_MSG";
            Class clazz = StateCode.class;
            Field[] fields = clazz.getFields();
            for (Field field : fields) {
                String fieldName = field.getName();
                for (String key : proper.stringPropertyNames()) {
                    if (fieldName.equals(key)) {
                        field.setAccessible(true);
                        field.set(clazz, proper.getProperty(key));
                        codeMsgMap.put((String) field.get(clazz), proper.getProperty(fieldName + codeMsgSuffix));
                        break;
                    }
                }
            }
            LOGGER.debug("Code Size :" + fields.length);
            LOGGER.debug("codeMsgMap Size :" + codeMsgMap.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getMsg(String code) {
        return codeMsgMap.get(code);
    }


}